package com.apes.hr.group.model;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.hr.base.model.WorkPost;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能：钉钉部门关联（公司部门岗位）表
 * @author ZhengZX
 * @create 2020年9月15日11:05:36
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DingDingDeptRelevance.CONST_TABLE_NAME)
public class DingDingDeptRelevance extends Model {
    public static final String CONST_TABLE_NAME = "hr_ding_ding_dept_relevance";

    @Id
    @Column(name = "id", nullable = false, length = 120)
    @Comment("编码")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "钉钉部门关联表", padding = 8, prefix = "D")
    private String id; //编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ding_ding_dept_id", referencedColumnName = "id")
    private DingDingDept dingDingDept; //钉钉部门群

    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
    @JoinTable(name = "hr_work_post_ding_dept",
            joinColumns = {@JoinColumn(name = "ding_dept_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "work_post_id", referencedColumnName = "id")},
            indexes = {@Index(name = "idx_ding_dept_work_post", columnList = "ding_dept_id,work_post_id")}
    )
    private List<WorkPost> workPosts = new ArrayList<>();   //岗位

    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
    @JoinTable(name = "hr_company_ding_dept",
            joinColumns = {@JoinColumn(name = " ding_dept_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "company_id", referencedColumnName = "id")},
            indexes = {@Index(name = "idx_ding_dept_company", columnList = "ding_dept_id,company_id")}
    )
    private List<PartyRole> partyRoles = new ArrayList<>();   //公司

    @ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY)
    @JoinTable(name = "hr_branch_ding_dept",
            joinColumns = {@JoinColumn(name = "ding_dept_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "branch_id", referencedColumnName = "id")},
            indexes = {@Index(name = "idx_ding_dept_branch", columnList = "ding_dept_id,branch_id")}
    )
    private List<Branch> branches = new ArrayList<>();   //行政部门

    // 定义岗位前端界面转化
    @Transient
    private List<JSONObject> workPostList;

    // 定义公司前端界面转化
    @Transient
    private List<JSONObject> companyList;

    // 定义行政部门前端界面转化
    @Transient
    private List<JSONObject> branchList;


}
